﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TimeTravel.XnaLibrary.UI
{
	public class FadingLabel : Label
	{
		private TimeSpan mTimePassed;
		private TimeSpan mTimeToDisappear;

		public override string  Text
		{
			 get 
			{ 
				 return base.Text;
			}
			 set 
			{ 
				base.Text = value;
				mTimePassed = TimeSpan.Zero;
				this.IsVisible = true;
			}
		}

		public FadingLabel(SpriteFont font, TimeSpan timeToDisappear) : base(font)
		{ 
			mTimeToDisappear = timeToDisappear;
			IsVisible = false;
		}

		public void Update(GameTime gameTime)
		{
			if (!this.IsVisible)
			{
				return;
			}			

			mTimePassed += gameTime.ElapsedGameTime;
			Color textColor = TextColor;
			textColor.A = (byte) ((1 - mTimePassed.TotalSeconds / mTimeToDisappear.TotalSeconds) * 255);
			TextColor = textColor;
			if (mTimePassed > mTimeToDisappear)
			{
				this.IsVisible = false;
			}
		}	
	}
}
